How to create Raster statistics with GDAL externally?

As a workaround you can use command

gdalinfo -stats -hist input.tif

Selecting the histogram option triggers the creation of input.tif.aux.xml file.

EDIT

I have learned that actually there is no need to use the -hist workaround. There is a GDAL configuration option GDAL_PAM_ENABLED that forces the creation of the .aux.xml file. Usage example:

gdalinfo test.tif -stats --config GDAL_PAM_ENABLED TRUE

The following isn't a direct GDAL solution for your question, but it might help. The Orfeo Toolbox command otbcli_ComputeImagesStatistics can be used to generate raster band statistics and output them to an XML file. For instance:

otbcli_ComputeImagesStatistics -il input_image.tif -out output.xml