IMU axis rotation, calculate values with respect to Earth plane
The IMU should have the same axes for the accelerometer, gyroscope and whatever else there is inside. Just look it up in the datasheet.

My approach to this would be to first make sure that the axes of your IMU are alligned with the axes of your vehicle since the IMU can only measure the velocity, euler angles and so on of itself.

To get the euler angles from your IMU you can use this:

https://robotics.stackexchange.com/questions/4511/euler-angles-from-9dof-imu

assuming your IMU has a Magnetometer. Otherwise there are many tutorials on how to get the roll, pitch and yaw angle from IMUs:

https://engineering.stackexchange.com/questions/3348/calculating-pitch-yaw-and-roll-from-mag-acc-and-gyro-data

After this, you already have the euler angles you need and you can rotate the vectors you want (acceleration and angular velocity) into the earth frame. This can be done with quaternions as you mentioned. For this there are also a lot of tutorials online. The madgwick paper where you got the algorithm from has a good explanation for this problem.

So to clarify:
You calculate the orientation of your reference frame relative to your world frame (Euler angles) with your IMU. Then you can rotate whatever vectors your IMU gives you (acceleration,  angular velocity, ...) from your reference frame into the earth frame and use them (calculate trajectory, compensate gravity, calculate movement etc.)