Depends on how 'realistic' you want to be.
We all know that in SH4 it's fairly easy to get the sort of score that would have seen you being voted in as the next President of the US. But that's because there is no danger in taking chances with your virtual sub. Which is why SH tonnage scores are usually way in excess of the real figures.
In real life of course things are very different; even if you personally had the balls (or stupidity) to choose an engagement in shallow water, as opposed to picking a place to engage where you have a better tactical advantage. I doubt very much whether your crew would appreciate you risking their lives in such a manner, and responsibility towards the welfare of your crew, and your boat is an imporant consideration too, even in a simulation.
So if you want to emulate a realistic sub patrol, here are a couple of quotes from Sun Tzu's The Art of War which might prove useful...
'The Commander stands for the virtues of wisdom, sincerity, benevolence, courage and strictness.'
'With his forces intact he will dispute the mastery of the Empire, and thus, without losing a man, his triumph will be complete. This is the method of attacking by stratagem.'
Notice that SunTzu lists wisdom before courage! In other words, sometimes it is better to leave a target alone and go for a better opportunity. A glorious five minute battle which results in the loss of your sub is not a good substitute for a boat which survives to fight again and again!
Not everything in The Art of War stands up as a paragon of wisdom of course, but there are lots of bits of it which are useful to adopt. However, if you prefer a couple of assesments of the smart way to fight a war from more recent times, try these:
Douglas Bader's take on things: 'War is not a game of cricket.' - by which he meant that offering the enemy a sporting chance to shoot back is alright for sport, but in war the reality is that it's better to shoot someone in the back while they aren't looking if you want to win.
Manfred Von Richtofen: 'It's the job a fighting pilot to destroy the enemy, anything else is rubbish.' - by which he meant that although he was often accused of taking on planes which were at a disadvantage, and refusing to give people a 'sporting chance', it was not his job to be sporting, it was his job to kill people and survive to be able to do it again and again. And if that meant nailing some poor RFC flyer with a few hours of flight experience flying an inferior aeroplane, that that was their tough luck. If his enemy was at a tactical advantage, then it made perfect sense to not even get into a fight in the first place.
So the biggest tip I can suggest is to choose your battleground more wisely, and then kick the S.O.B.s while they aren't looking.
The scenario in the original post is one where you would have to weigh up the pros and cons of sinking a ship and risking your sub, versus passing it by and looking for a target where the outcome was more assured, and then being in a position to do it again and again.
I'd personally go for living to fight another day and the possibility of more tonnage for less risk. Engaging in shallow water with destroyers about is just asking for trouble. Especially as, in real life, the enemy wouldn't even need sonar, they'd be able to look over the side and see you under the water, especially with help from an aeroplane, which you could be damn sure they would radio for.
Therefore, the best way to avoid being attacked in shallow water, is to not get into a fight in shallow water.