That's dependent on which phone you have and what software you have on that phone.
Here's a few places to start:
And samsung just won't unlock US models.
And xiaomi factory resets the phone when relocking.
Searching for relock root android gives very unhelpful results in general.
I did learn there's one specific model of motorola where the "lock" just doesn't work, and that's amusing but not very useful.
So I meant your phone specifically. Not just rooting, but rooting with a currently-locked bootloader. Please tell me a little bit about an example where this actually works. And hopefully passes safetynet, and will keep passing safetynet with hardware attestation.
I unlocked my bootloader (cf. https://forum.xda-developers.com/t/guide-s-off-root-your-htc...), installed TWRP[0] and then re-locked my bootloader.
I then variously installed different custom roms (LineageOS, NuSense, ResurrectionRemix) and in between re-flashed the stock rom.
I used SuperSU and AddonSU to root the installed images. Magisk is also a popular rooting tool (AIUI, the MagiskHide component blocks Google Play from identifying the device as rooted as well).
Once I installed TWRP, I was able to do all of those things with a locked bootloader and had zero problems running any Android application.
I'd note that LineageOS[1] supports hundreds of different phones.
Any reasonably tech savvy person can do this fairly easily and a non-tech savvy person can do so with a modicum of effort.
If you're actually interested, I'll happily sell you my services to do this on your phone.
And I promise you that I will charge you an outrageously large amount for those services. It's definitely not worth it, but I'd be happy to take your money.
[0] https://twrp.me/