Keep in mind that if they are even stepping in water they are able to do the stun attack. So if you are in the swamp and you see a bary then avoid it or kill it fast enough that it doesn't stun both you and your tame resulting in you getting dismounted.

More Baryonyx Encountering Tips