I had this problem fairly regularly with my previous (Intel) MBP (though it hasn't shown up since I upgraded to an M1 Max). I eventually figured out a reliable solution for it:
1) Under the Window menu, select the window that's gone invisible
2) Minimize it (using the Cmd-M shortcut or the Window menu item), causing it to become visible in the Dock
3) Click on it in the Dock, un-minimizing it and making it visible again
Now, I can't guarantee this would work generally (I was only having the problem with a particular Safari window that I regularly kept in split-screen with Music on my external monitor), but it did work consistently for my particular issue.