x-magento-init is the proper way to run some JavaScript code from a *.phtml template.
The official documentation refers such JavaScript code as a JavaScript component.
You can pass some parameters from PHP to JavaScript with x-magento-init.
An example from the core:
The code pass 3 parameters to the JavaScript component: attributesData, isSortable, isReadOnly.
How can the parameters be accessed by the component? Let’s look at its code:
As you can see, the component is an AMD module.
The module returns a function and the parameters are passed to the function like an object (named config in this case).