Yes, you can treat it as a hyperparameter.   I prefer to think of it as a design decision.   If you want to take advantage of transfer learning, then you will probably be copying someone else’s pre-trained model, which means the nheads is already fixed to whatever that someone else has chosen.  However, if you have several such pre-trained models to choose from, you can try them all and pick the optimum.
For example, if you want to use BERT, there are many different pre-trained models (Base, Large, Tiny, Small, Medium) as well as several pre-trained BERT-like variations (DistilBERT, RoBERTa, ALBERT).  You can check out the nheads and the nlayers for any of those.
For my analysis, I used BERT-base and then I compared it with DistilBERT.   The BERT-base gave slightly better values, but took much longer to train.  This is pretty much what the DistilBERT authors claim.
Up to a point, more heads is likely to mean better results, so long as you have enough data.   However, if a BERT-small model has enough parameters to learn all the signal that there is in the data, then adding more heads (for example by using BERT-base) won’t be able to do any better.