In case of ARM, the Cortex-M microcontrollers have common processor core, debug interface, interrupt controller (NVIC) and systick timer. The rest of the peripherals are added by uc vendors. There are some exceptions (ie multiple vendors use USB OTG core licensed from Synopsys), but usually the peripherals are developed in-house and are unique to that vendor. So moving within the same manufacturers microcontrollers, the peripherals are similar, but otherwise not.
In case of switching vendors, is is usually easier to keep the same architecture (ARM Cortex-M). The difference between ie PIC and AVR is greater than ST ARM and NXP ARM. You already know how how the CPU operates, how to debug it, the interrupt controller works etc. Especially expect a larger step when moving from 8-bit controllers with very simple peripherals to 32-bit controllers with much more complex peripherals, DMAs etc.
About the complexity of moving between manufacturers... If you know how the UART works, then it is not too difficult to move to another vendors UART. The operational principle is still the same and the register interface to expect from it is usually quite similar. Things get more difficult if you intend to use DMA and other advanced functionality. To make porting easier, it is wise to write your software in layers. Then you can swap the closest to hardware layer (that encapsulates all register accesses) and the rest of the code does not need to be changed.
Using STM32: in my opinion, the HAL is horrible piece of code. I'm using CubeMX only for pin-planning (it makes it easier to find combinations of input muxing so that all required peripherals can be connected up - in case of larger controllers same peripheral connection can be muxed to different pins on the case). After that, I go bare-metal and write my own hardware layer. In case of STM32, I've used already most of the peripherals, so it is just porting hardware layer code written for earlier projects. The HAL can be used as a reference, just look into it how ST wrote the registers.