For a Uboat any warship was a target of opportunity so, unless taking action against it would endager the primary mission or pose unecessary risks, there was no prohibition.
Of course, if you're Gunter Prien and you're tasked to sneak into Scapa Flow you won't stop to sink every damned destroyer or armed trawer that happens to pass nearby
That's just common sense!