It's quite a bit of physics. Wave propigation and hydroacoustics are key here, I suggest you familiarize yourself with the following:
The issue with acoustics is that most of the stuff is passive stuff, so unless you make no noise, it'll register. And you will always make noise.
Main sound from subs is friction from the water and the cavitation of propellers blades - there are low pressure areas behind the blades that generate bubbles which pop causing noise. That's why they've got plastic tiles, and main systems are vibration isolated for the hull.
As with that, depending on your SNP gradient and local conditions (Convergence, Surface duct, bottom bounce, sound channels), you could theoretically hide under the critical angle of reflection. The three areas most likely to hide are at the bottom of the ocean, near the top (surface duct), and in the isothermal sound channel.
Surface zones are not always viable because you can use thermal imaging to see subs underwater up to around 40 meters (140 feet ish) supposedly new tech goes to 75 meters
, alongside day/night/seasonal variations. Sound duct isn't always viable because it is not always present. and bottoming out isn't viable if there's a bottom bounce (esp if its rocky) OR if its too deep.
In addition, if the aircraft has MAD (Magnetic anomaly detectors) you're shit outa luck if they do a flyby and notice the MAD signature fluctuate from the regular earth background noise. The most deadly weapon against subs is a helo or dedicated ASW plane.
For passive sonar, your best bet is to make your SNR ratio extremely low, since usually they focus on certain specific "bands" of waves that are constant (like propeller pitch, or engine noise). For example, bands above 1000 Hz range are basically guaranteed to be manmade. Because lower frequencies tend to propagate further, usually the first contact you'll see is a <100HZ band (The Russians are notorious for a 50Hz band on their subs). For example, the Soviet 670 project (Charlie I) has a band ID of 50, 235, 325, 868, 1719, the French Améthyste (Rebuild of the Rubis series) has a band ID of 60, 250, 325, 1142,1839.
You also have to take into consideration, that surface vessels conducting ASW are usually at a disadvantage due to the surface noise vs a sub (but they get helos). Sonar washout is also an issue (the Russians again, are notorious for this). Washout speed for sonars vary, but a ship traveling at 10-15kts is usually going to have a hard time with ASW in rough seas.
Theoretically speaking, some sort of stirling engine would probably reduce noise pretty decently. Maybe mix it with the liquid ox cells that the Sweeds/Germans use. Also, use a snorkel because fucking radar is a thing.
>how to counter sonar
For passive, Stormy seas are your friend, alongside certain oceans (Baltic, arctic oceans versus the relatively still Mediterranean/ Pacific/US gulf coast), knowing your geography, and slow running.
For active? Hope you have a sound channel, or they are moving so fast they can't hear you above sound to noise ratio.