I just moved your thread to “Developers” category as the issue arose when you added your own custom code.
I tried your code locally but was unable to replicate the issue.
Could it be other plugin’s code causing that? $tag should be string but not sure why it’s an Array in your case.