Is it safe to switch from Yoast to RankMath? Would that cause any harm?
Switching from one site map plugin to another will always create a headache as you basically resubmitting your site map causing Google bot(s) to re-crawl the whole website again. In the short term, you'll see rank fluctuations, in the long, I don't know. When it comes to a site map plugin best is to do your homework and stick to one for the long run.
Personally I have had a love-hate relationship with Yoast, their updates are always untested, unstable and a loose cannon. You'll never know what to expect. It's going to be a hit or miss creating havoc. The guy is basically crazy Dutch with compete disregard how his updates will affect the end-user. He doesn't give a shit. His initial success went over his head and he is trying to ride the wave.
I used to be Yoast paid subscriber until things went terribly wrong, asked them to give me access to previous version which they did (not without blaming me for their shit update), and canceled my subscription. I still use it as I don't want to confuse Google or resubmit my site map, but it's a stable version from a couple of years ago. It works fine for me.
As for RankMath, I'm not sure about that, to fully enjoy the benefits you have to create an account with details about your website. That scared the shit out of me as I don't know how they might use it. Love or hate Yoast, they don't ask for such info. Got to give them a credit for that. If you use Yoast DO NOT update until you read users' feedback. If you see negative comments, sit back and wait and always have a backup in case things go wrong so you can revert to the previous version.