I put in place a 301 redirection for the add-on domain
subdomain.example, because Google was sometimes indexing
Is this 301 redirection the right thing to do for SEO?
Yes, 301 redirecting is the right thing to do, though you should take care to not just redirect everything to the homepage - for example,
subdomain.domain.example/page needs to redirect to
You should also check to see if you can find any sites that link to the add-on (undesired) version and ask them to update their links to the simpler (desired) version, as links are what Google uses to find sites and are probably the reason the undesired version got indexed.
Finally, make sure to check your CMS / your site itself for any references to the add-on (undesired) version. Your own site may be referencing the undesired URL and it is worth fixing both for ensuring Google and others find the right site, but also so visitors aren't waiting on constant redirects to resolve.