I had the same idea at some point in Legion and tried what you are trying to do aswell. Turns out there isn't really a clean and simple solution to it. If you want to read a bit more check this
thread
The blacklist idea from my last post in the linked post, resulted in big performance issues. After this i gave up on this idea.
If you really want this feature, my advice is to stick to an established nameplate addon. There are plenty of them out there. My personal recomendation in this case would be
Plater Nameplates.