There is a way! I wrote this tool exactly for this purpose, and works on both x11/wayland: codeberg.org/tubbadu/ktoggle
it writes a kwin script on a temporary file, installs it, runs it and uninstalls it. All using DBUS communication.
it works quite well (there is just rarely an unexpected behavior I didn’t find the time to fix yet), I use it daily for lots of apps like telegram, whatsapp, qalculate and a note taking app
once compiled and installed you can just assign to a hotkey the command
It was the best way I found to do it, because I couldn’t find a proper way to run a kwin script with an argument (the program name and class), so I decided to just modify the script and then install and run it, and uninstall at the end. “Install” only takes a blink, although it may be made faster, for example implementing a sort of “caching” of the last used script in order to avoid removing and reinstalling the same script, perhaps one day I’ll try to implement it. My first attempt at it was done using KWindowSystem api, but something didn’t work so I had to give up and just install the scripts. perhaps I can try again with PlasmaWindowManagement, I may be luckier