NJ: If you mean having a transformer (LV) within an enclosure with a contactor, with a UL or CSA listing, then it would be acceptable. The wiring methods would have to be compliant also.
The OP I believe is refering to a transformer for a doorbell.
An octagon box mounted in a closet or panel room. Use a cover with 1/2" ko. Line voltage stays in the box. LV gets popped out the drywall next to the box. Transformer is exposed. It doesn't look great but in a closet or mechanical room its not bad.
We have been using the self contained ones for a few years now. Transformer fits in a deep 1 gang box and chime installs to that box at trimout. Transformer is mounted to a steel plate with holes for the LV wires to stick out, the line is contained in the 1 gang and the LV is all outside of it.
as I noticed above .. the biggest problem of burying anything requiring connections is a no-no .. After 10 years or so that transformer will go bad and then what. Readily accessible is the key I'm pretty sure ..")
Anyone claiming to know everything about Electrical, is wrong.