This doesn't sound legal, and if it is it shouldn't be.
It’s not, they were sued for this practice a few years ago. They continue to do it.
My most recent printer is an HP I bought at Costco because I knew it worked with CUPS and HPLIP on Linux and macOS. Given how many times I've seen HP pull this stunt though, I'll most likely shop for a different brand next time.
Part of this is also that HP has captured a LOT of corporate printing locations. The cost of migration for those businesses, universities and organizations is huge. I think that is why they keep getting away with this. Even if individual consumers move away from HP, they still own a huge chunk of the corporate printing market.