I don’t think that they necessarily knew, it’s a scatter gun approach, if they call enough people, some will have current dealings with HMRC.
pits like if you get an email from the bank and you don’t bank with them you know it’s a scam and ignore it, but if you happen to bank with them and have current dealings you can get drawn in.
the answer is never respond to anything, but call or email on a different device to contact details looked up separately, and still take care