Water heater question

I have seen several houses that have an electric water heater in a bedroom closet. The master bath is on the other side of the wall. Is this allowed or even safe.

Not an issue. Just make sure they have a pan and the supply cord is protected so when they pile clothes on top of it, it doesn’t get damaged.