Unfortunately the answer for rooting temporarily without data loss is no – by design. Rooting e.g. with Magisk requires a modification to the boot partition. Due to verified boot your phone would not boot anymore (and therefore you can’t simply change such things normally).
It is part of the security model that a bootloader unlock wipes the device. Else an unlock would give huge possibilities for local attacks.
Zeno There must be a WORKAROUND.
Please
If there was a workaround for the rightful owner, there would be the same workaround for an attacker.
The only way you could achieve (root) access to block devices without wiping everything would be using a vulnerability with existing exploit like… for example CVE-2022-0847 (this will example obviously NOT work anymore if it ever worked outside the stock OS). Exploiting GOS is significantly harder anyway → That is the point of GOS.
The already mentioned TRIM function severely limits the usefulness of imaging a solid state device (this works pretty good for HDDs on a computer for trying PhotoRec or similar).
As harsh as it sounds the solution for the problem is: “Backup your files regularly.”