Digital optical connection is a very limited as to what formats it can pass. It can pass stereo pcm signals of very high quality. It can also pass multichannel dolby digital and dts 5.1 or 6.1 tracks encoded in a compressed lossy Bitstream format like those used in dvd's.
Hdmi can pass all the above plus 8channel pcm in high quality and also the new lossless high res dolby and dts standards as well as atmos and dts:x.
For bluray sources hdmi is recommended but it has one big downside and that is the AV receiver in between the player and the tv needs to be new enough to support the the hdmi features being passed. Older receivers may not have hdmi switching or their switching does not support 3d, 4k or whatever and then you may have to connect your source and tv directly and use digital optical connection from source or tv to the AVR to pass sound.
The new lossless multichannel audio only hdmi will pass may have much more info but as far as quality you can actually hear the difference of is not much and you will need a very good system and room to appricate this difference. But hdmi reduces the number of cables and makes things a bit simpler plus it is the only way to get new formats like atmos.